package com.headingdata.exercise;

public class Test4 {
    public static void main(String[] args) {
        //四位数字密码加密：如1983,加密规则：每位数+5再对10求余，最后所有数字反转，得到一串加密后的新数
        String result = encryption(1983);
        String result1 = encryption(8346);
        System.out.println(result);
        System.out.println(result1);
    }
    public static String encryption(int number){
        int[] numbers = split(number);

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (numbers[i] + 5) % 10;
        }
        reverse(numbers);
        String result = "";
        for (int i = 0; i < numbers.length; i++) {
            result += numbers[i];
        }
        return result;
    }

    //数字分割成数组
    public static int[] split(int number){
        int[] numbers = new int[4];
        numbers[0] = number/1000;
        numbers[1] = (number/100)%10;
        numbers[2] = (number/10)%10;
        numbers[3] = number%10;
        return numbers;
    }

    //数组反转
    public static void reverse(int[] numbers){
        for (int i = 0,j =numbers.length-1; i < j; i++,j--){
            int temp = numbers[i];
            numbers[i] = numbers[j];
            numbers[j] = temp;
        }
    }
}
